package
{
	import flash.desktop.ClipboardFormats;
	import flash.desktop.NativeDragManager;
	import flash.desktop.NativeProcess;
	import flash.desktop.NativeProcessStartupInfo;
	import flash.display.Sprite;
	import flash.events.NativeDragEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	
	import mx.controls.Alert;
	import mx.states.RemoveChild;
	
	[SWF(width="800",height="600",backgroundColor="#F022FF")]
	public class ExcelToXml extends Sprite
	{
		private var bg:Sprite;
		private var text:TextField;
		
		private var handlerFiles:Array;
		
		var format1:TextFormat;
		
		private var descriptionSprite:Sprite;
		private var descriptionText:TextField;
		private var confFile:File;
		
		public function ExcelToXml()
		{
			init();
		}
		
		private function init():void
		{
			//build bg
			bg = new Sprite();
			bg.name = "bg";
			bg.graphics.beginFill(0xFFFFFF);
			bg.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
			bg.graphics.endFill();
			addChild(bg);
			
		
			text = new TextField();
			text.autoSize = TextFieldAutoSize.CENTER;
			text.htmlText = "<strong>拖进Excel</strong>"+"<br/>批处理excel的要求<ul><li>"+"Sheet名称统一为output";
			text.x = stage.stageWidth/3;
//			text.y = stage.stageHeight;
			format1= new TextFormat();
			format1.font="黑体";
			format1.size=29;
			text.setTextFormat(format1);
			bg.addChild(text);
			
			descriptionSprite = new Sprite();
			descriptionSprite.x = 10;
			descriptionSprite.y = text.height;
			bg.addChild(descriptionSprite);
			
		
			
			handlerFiles = new Array();
			
			confFile = new File();
			
			addListener();
		}
		
		
		public function addListener():void {
			bg.addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER,onDragIn);//通常做拖入文件的类型检查	
			bg.addEventListener(NativeDragEvent.NATIVE_DRAG_DROP,onDrop);//拖拽完成事件
		}
		
		private function removeListener():void{
			bg.removeEventListener(NativeDragEvent.NATIVE_DRAG_ENTER,onDragIn);//通常做拖入文件的类型检查	
			bg.removeEventListener(NativeDragEvent.NATIVE_DRAG_DROP,onDrop);//拖拽完成事件
		}
		
		private function onDragIn(e:NativeDragEvent):void
		{
			trace("onDragIn");
			clear();
			//判断拖动的内容是否为文件
			if (!e.clipboard.hasFormat(ClipboardFormats.FILE_LIST_FORMAT))
				return;
			var fileArr:Array = e.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;
			for each(var file:File in fileArr)
			{
				trace(file.name);
				switch (file.extension.toLowerCase())
				{
					case "xlsx":
					case "xls":
						trace(file.url);
						handlerFiles.push(file);
						//如果为文件则允许拖入
						NativeDragManager.acceptDragDrop(bg);
					break;
				}
			}
			
			if(handlerFiles.length ==0)
			{
				trace("没有可以处理的execl");
				alterMessage("没有可以处理的execl");
			}
		}
		
		private function alterMessage(text:String):void
		{
			descriptionText = new TextField();
			descriptionText.width = stage.stageWidth;
			descriptionText.wordWrap = true;
			descriptionText.htmlText = text;
			var desFormat:TextFormat = new TextFormat();
			desFormat.size=20;
			descriptionText.setTextFormat(desFormat);
			descriptionSprite.addChild(descriptionText);
		}
		
		private function clear():void
		{
			for(var i:int=0;i<descriptionSprite.numChildren;i++)
			{
				descriptionSprite.removeChildAt(0);
			}
		}
		
		private function onDrop(e:NativeDragEvent):void
		{
			trace("onDrag");
			if(handlerFiles.length ==1 )
			{
				singleFile();
			}
			else if(handlerFiles.length>1)
			{
				trace("mutiFile");
				multiFileShow();
			}
			else
			{
				var fileText:TextField = new TextField();
				fileText.text = "没有可以处理的execl";
				fileText.setTextFormat(format1);
				descriptionSprite.addChild(fileText);
			}
		}
		
		private function singleFile():void
		{
			trace("singleFile");
			var file:File = handlerFiles[0];
//			var showFile:Sprite = new Sprite();
//			showFile.x = 10;
//			showFile.y = text.height;
//			showFile.width = stage.stageWidth;
//			showFile.height = stage.stageHeight;
//			showFile.graphics.beginFill(0xFFab00);
//			showFile.graphics.drawRect(0, 0, showFile.width, showFile.height);
//			showFile.graphics.endFill();
//			bg.addChild(showFile);
			var nameText:TextField = new TextField();
			nameText.text = "生成的文件名称：";
			nameText.x = 10;
			nameText.y = text.height+10;
			
			var nameInput:TextField = new TextField();
			nameInput.x = nameText.x+100;
			nameInput.y = nameText.y;
			
			nameInput.width = 100;
			nameInput.height = 20;
			nameInput.autoSize = TextFieldAutoSize.LEFT;
			nameInput.border = true;
			nameInput.borderColor = 0x0000ff;
			nameInput.type = TextFieldType.INPUT;
			nameInput.text = file.name;
			
			
			
			
			descriptionSprite.addChild(nameText);
			descriptionSprite.addChild(nameInput);
			
			var sheetInput:TextField = new TextField();
			
			descriptionSprite.addChild(sheetInput);
			
			handlerFile(file);
		}
		
		var log:TextField = new TextField();
		private function traceLog(text:String):void
		{
			log.x = 10;
			log.y = 500;
			
			log.width = stage.stageWidth;
			log.wordWrap = true;
			log.height = 20;
			log.autoSize = TextFieldAutoSize.LEFT;
			log.border = true;
			log.borderColor = 0x0000ff;
			log.text = log.text +"||" +text ;
			descriptionSprite.addChild(log);
		}
		
		private function handlerFile(file:File):void
		{
			
			var newFile:File = File.applicationDirectory;
			trace(newFile.url);
			traceLog(File.applicationDirectory.nativePath);
			newFile = File.applicationStorageDirectory;
			trace(newFile.url);
			traceLog("applicationStorageDirectory"+newFile.url);
			
			newFile.nativePath =File.applicationDirectory.nativePath+ "/execl/"+file.name;
			trace(newFile.url);
			traceLog("nativePath"+newFile.url);
			
			file.copyTo(newFile,true);
			
			
			confFile.nativePath = File.applicationDirectory.nativePath + "/conf/excel.xml";
			var fileStream:FileStream = new FileStream();
			fileStream.open(confFile, FileMode.READ);
			var confXML:XML = XML(fileStream.readUTFBytes(fileStream.bytesAvailable));
			fileStream.close();	
			trace(confXML);
			
			handlerConfXML(confXML,file);	
			
		}
		
		
		private function handlerConfXML(xml:XML,file:File):void
		{
			var fileName:String = file.name.substring(0,file.name.indexOf("."));
			var beanId:String = fileName;
			var xmlHandler:ConfXmlHandle = new ConfXmlHandle(xml);
			var beansXML:XMLList = xml.elements();
			var beanXML:XMLList = beansXML.(@id == beanId);
			var newBean:XML = <bean></bean>;
			if(beanXML.length()!=0)
			{
				newBean = beanXML[0];
				var proElms:XMLList = newBean.elements();
				var filePro:XMLList = proElms.(@name == "file");
				var value:String = "execl/"+file.name;
				filePro[0].@value = value;
				
				
				
			}else{
			newBean.@id = beanId;
							
			newBean["@class"] = "com.sean.excel.ExcelObject";
			var name:String = "file";
			var proElm:XML = <property name={name} value={value}></property>;
			newBean.appendChild(proElm);
			
			
			name = "sheetname";
			value = "output";
			proElm = <property name={name} value={value}></property>;
			newBean.appendChild(proElm);
			
			name = "outfile";
			value = "output/as/"+fileName+".xml";
			proElm = <property name={name} value={value}></property>;
			newBean.appendChild(proElm);
			
			name = "javaxmlfile";
			value = "output/java/"+fileName+".xml";
			proElm = <property name={name} value={value}></property>;
			newBean.appendChild(proElm);
			
			name = "beginrow";
			value = "2";
			proElm = <property name={name} value={value}></property>;
			newBean.appendChild(proElm);

			name = "classname";
			value = "knight.basevo."+fileName;
			proElm = <property name={name} value={value}></property>;
			newBean.appendChild(proElm);
			
			name = "sheetname";
			value = "output";
			proElm = <property name={name} value={value}></property>;
			newBean.appendChild(proElm);
			}
			
			xml.appendChild(newBean);
			
			trace(xml);
			
			var allobject:XMLList = beansXML.(@id == "allobjects");
			var props:XMLList = XML(allobject[0]).elements();
			var list:XMLList = props[0].elements();
			var refs:XMLList = list[0].elements();
			
			if(refs.length()!=0)
			{
				var refName:XMLList = refs.(@bean==beanId)
				if(refName.length()==0)
				{
					var newRef:XML = <ref bean={beanId}/>;
					list.appendChild(newRef);
				}
			}
			else
			{
				var newRef:XML = <ref bean={beanId}/>;
				refs.appendChild(newRef);
			}
			writeXML(xml);
			
			callBat();
		}
		
		private function writeXML(xml:XML):void
		{
			var fileStream:FileStream = new FileStream();
			fileStream.open(confFile, FileMode.WRITE);
			fileStream.writeUTFBytes(xml.toXMLString());
		}
		
		private function callBat():void
		{
			var cmdFile:File=new File();
			cmdFile = cmdFile.resolvePath("C:/WINDOWS/system32/cmd.exe");
			var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
			nativeProcessStartupInfo.executable = cmdFile;
			
			var processArgs:Vector.<String> = new Vector.<String>();
			processArgs[0] = "/c "+File.applicationDirectory.nativePath+"/cmd.bat";
			nativeProcessStartupInfo.arguments=processArgs;
			var process:NativeProcess = new NativeProcess();
			process.start(nativeProcessStartupInfo); 
		}
		
		private function multiFileShow():void
		{
			var showFile:Sprite = new Sprite();
			showFile.x = stage.stageWidth/2;
			showFile.y = stage.stageHeight/2;
			showFile.width = 300;
			showFile.height = 300;
			showFile.graphics.beginFill(0xFFFF00);
			showFile.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
			showFile.graphics.endFill();
			bg.addChild(showFile);
			var fileText:TextField = new TextField();
			fileText.text = "需要处理的文件有:";
			fileText.setTextFormat(format1);
			
			showFile.addChild(fileText);
			
			for(var i:int=0;i<handlerFiles.length;i++)
			{
					var file:File = File(handlerFiles[i]);
					fileText = new TextField();
					fileText.text = file.name;
					showFile.addChild(fileText);
					
					
			}
				
		}
		
	}
}